package com.dd.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.*;

public class DictDAO extends HibernateDaoSupport  {
	private static final Log log = LogFactory.getLog(DictDAO.class);
	
	public void saveOrUpdate(Dict transientInstance) {
		log.debug("saving Dict instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public Dict findById(java.lang.Integer id) {
		log.debug("getting Dict instance with id: " + id);
		try {
			Dict instance = (Dict) getHibernateTemplate().get(
					"com.dd.entity.Dict", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public List findByRootNode(int root) {
		log.debug("finding Dict ByRootNode");
		try {
			String queryString = "from Dict where root_node=" + root
					+ " order by id";
			return  getHibernateTemplate().find(queryString);
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public List findByType(int type) {
		log.debug("finding Dict ByRootNode");
		try {
			String queryString = "from Dict where node_type=" + type
					+ " order by id";
			return  getHibernateTemplate().find(queryString);
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public List findByNodeCode(int nodeCode) {
		log.debug("finding Dict ByRootNode");
		try {
			String queryString = "from Dict where node_code=" + nodeCode
					+ " order by id";
			return  getHibernateTemplate().find(queryString);
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public Dict findByTypeAndCode(int nodeType,int code) {
		log.debug("finding Dict ByTypeAndCode");
		try {
			String queryString = "from Dict where node_type=" + nodeType
					+ " and node_code="+code+" order by id";
			List li =   getHibernateTemplate().find(queryString);
			if(li.size()!=0){
				return (Dict)li.get(0);
			}else{
				return null;
			}
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public List findByTypeAndRoot(int nodeType,int root) {
		log.debug("finding Dict ByTypeAndCode");
		try {
			String queryString = "from Dict where node_type=" + nodeType
					+ " and root_node="+root+" order by id";
			return getHibernateTemplate().find(queryString);
		
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public Dict findByTypeAndCode(int nodeType,int root,long code) {
		log.debug("finding Dict ByTypeAndCode");
		try {
			String queryString = "from Dict where node_type=" + nodeType
					+ " and node_code="+code+" and root_node="+root+" order by id";
			List li =   getHibernateTemplate().find(queryString);
			if(li.size()!=0){
				return (Dict)li.get(0);
			}else{
				return null;
			}
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public Dict findByTypeAndValue(int nodeType,String value) {
		log.debug("finding Dict ByTypeAndCode");
		try {
			String queryString = "from Dict where node_type=" + nodeType
					+ " and node_value='"+value+"' order by id";
			List li =   getHibernateTemplate().find(queryString);
			if(li.size()!=0){
				return (Dict)li.get(0);
			}else{
				return null;
			}
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
}
